import React from 'react';

const handleUnload = e => {
    e.preventDefault();
    // Chrome requires returnValue to be set
    // 现代浏览器已经无法定制消息
    e.returnValue = '当前页面的录音文件可能会丢失';
};

const usePreventUnload = (needPrevent = true) => {
    React.useEffect(() => {
        if (needPrevent) window.addEventListener('beforeunload', handleUnload);
        return () => {
            if (needPrevent) window.removeEventListener('beforeunload', handleUnload);
        };
    }, []);
};

export default usePreventUnload;
